#!/bin/bash
set -e
project_name="aims5"
backup_date="`date +%Y%m%d`"
source_dir=("/srv/lancet-aims-tsdb" "/srv/lancet-emr-runtime")
target_dir="/srv/backup/$project_name"
split_size="1000m"
keep_days=15
ftp_host=""
ftp_port="21"
ftp_user=""
ftp_pass=""


mysql_ip=192.168.0.211
mysql_container_name=lancet-aims-db
db_user=root
db_password=mysql@dcsoft
#增量备份天数
inc_day=6


backup_dir="/srv/backup/$project_name/lancet-aims-db"
history_dir="/srv/backup/$project_name/lancet-aims-db-lastweek"
base_backup_dir="$backup_dir/base"
inc_backup_dir="$backup_dir/inc"
#备份容器名
backup_container_name=percona-xtrabackup-8.4
#备份镜像明
backup_image_name=percona/percona-xtrabackup:8.4
#备份脚本
backup_script="docker run --name $backup_container_name --user 999:999 -v $backup_dir:$backup_dir -v $history_dir:$history_dir --volumes-from $mysql_container_name $backup_image_name xtrabackup --backup --datadir=/var/lib/mysql --user=$db_user --password=$db_password --host=$mysql_ip --port=3306"


if [ `whoami` != "root" ]; then
    echo "Please use the ROOT account to run the script."
    exit 1
fi
echo "Data volume backup, please wait ..."
start=`date +%s`
echo "[`date +'%Y-%m-%d %T'`] Starting backup ..."


upload_ftp () {
	file_name=$1
	echo "开始压缩文件：$file_name！"	
	mkdir -p $backup_dir/bzip2	
	cd $backup_dir	
	tar --warning=no-file-changed -czpPf - "$file_name" | split -db $split_size - "$backup_dir"/bzip2/"$file_name".tar.gz. >/dev/null	
    if [[ $ftp_host != "" && $ftp_user != "" && $ftp_pass != "" ]]; then
		echo "开始上传FTP：$file_name！"	
		ncftpput -u $ftp_user -p $ftp_pass -m -R -P $ftp_port $ftp_host $backup_date/lancet-aims-db $backup_dir/bzip2/$file_name.tar.gz.*
    fi		
}
bakcup_mysql () {	
	if docker ps -a | grep -q $backup_container_name; then
		echo "Container $backup_container_name exists."
		docker rm $backup_container_name
	fi
	mkdir -p $backup_dir	
    chown 999:999 $backup_dir
	cd $backup_dir
    #判断全库备份是否存在
    if [ -d $base_backup_dir ]; then
        #增量文件夹是否存在
        if [ "$(ls -d $inc_backup_dir* 2>/dev/null)" ]; then            
            #检查最后一次的增量备份
            last_inc_backup_dir=`ls -d $inc_backup_dir* | tail -1`
            last_inc_backup_dir=${last_inc_backup_dir/:/}
            #获取最后一次增量备份的数字
            last_inc_backup_num=${last_inc_backup_dir/$inc_backup_dir/}
            if [ $last_inc_backup_num == $inc_day ]; then
                #移动到历史目录
                rm -rf $history_dir
				mkdir -p $history_dir
				mv $backup_dir"/bzip2" $history_dir	
				rm -rf $backup_dir
                #重新备份
                bakcup_mysql
            else
                inc_backup_num=$(($last_inc_backup_num+1))
                echo "mysql第$inc_backup_num次增量备份"
				inc_backup_script=$backup_script" --target-dir="$backup_dir"/inc"$inc_backup_num" --incremental-basedir="$backup_dir"/inc"$last_inc_backup_num
                echo $inc_backup_script
                $inc_backup_script
				upload_ftp "inc"$inc_backup_num
            fi
        else
            echo "mysql第1次增量备份"
			backup_script_inc=$backup_script" --target-dir="$backup_dir"/inc1 --incremental-basedir="$base_backup_dir
            echo $backup_script_inc
            $backup_script_inc
			upload_ftp "inc1"
        fi
    else
        echo "mysql全库备份"
		all_backup_script=$backup_script" --target-dir="$base_backup_dir
        echo $all_backup_script
        $all_backup_script
		upload_ftp "base"
    fi
}

bakcup_mysql
echo "MySQL Data volume backup completed. ($((`(date +%s)`-start))s)"

for backup_dir_tmp in ${source_dir[@]}; do
    dest_name=${backup_dir_tmp##*/}
    dest_dir="$target_dir/$backup_date/$dest_name"
    dest_file="$dest_dir/$dest_name.tar.gz"
    mkdir -p $dest_dir
    cd /srv
    tar --warning=no-file-changed -czpPf - "$dest_name" | split -db $split_size - "$dest_file."
    cd - > /dev/null
    if [[ $ftp_host != "" && $ftp_user != "" && $ftp_pass != "" ]]; then
		ncftpput -u $ftp_user -p $ftp_pass -m -R -P $ftp_port $ftp_host $backup_date/$dest_name $dest_dir/*.tar.gz.*
    fi
done
find $target_dir -name "*" -type d -mtime +$keep_days -exec rm -rf {} \; > /dev/null 2>&1



echo "[`date +'%Y-%m-%d %T'`] Backup completed."
echo "Data volume backup completed. ($((`(date +%s)`-start))s)"
echo "-----------------------------------------"

exit 0